package cz.uhk.service;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import cz.uhk.domain.Bludiste;

public class SaveLoad {
	
	public static boolean saveBludiste(Bludiste b, File f) throws IOException{
		FileOutputStream fout = null;
		ObjectOutputStream oos = null;
		try {
		fout = new FileOutputStream(f);
		oos = new ObjectOutputStream(fout);
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		}
		
		try {
			oos.writeObject(b);
					
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
			
		}
		try {
			fout.close();
			oos.close();
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		}
		return true;
		
	}
	
	public static Bludiste loadBludiste(File f) throws Exception{
		if(f.exists()){
			FileInputStream fin = null;
			ObjectInputStream oin = null;
			try {
				fin = new FileInputStream(f);
				oin = new ObjectInputStream(fin);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				throw e;
			} catch (IOException e){
				e.printStackTrace();
				throw e;
			}
			
			try {
				Bludiste b = (Bludiste)oin.readObject();
				return b;
			} catch (IOException e) {
				e.printStackTrace();
				throw e;
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
				throw e;
			}
			
		}
		return null;
	}

}
